<?php
//пароль
if (!defined('INWEB')) return2();
$a=getVar('a');
includeLang('objects');
switch($a)
{
    case 'add':
        if($_POST)
        {
            $otype=getVar('object_type');
            $loc=getVar('loc');
            $desc=getVar('desc');
            $person=getVar('person');
            $manu=getVar('manufacturer');
            $model=getVar('model');
            $reg_nr=getVar('reg_nr');
            $reg_cert_nr=getVar('reg_cert_nr');
            $chassis=getVar('chassis_nr');
            $year=getVar('year');
            $comment=getVar('comment');
            $sql->query("INSERT INTO objects (`tId`, `loc`, `cta_person`, `desc`, `manufacturer`, `model`, `reg_nr`, `reg_cert_nr`, `chassis_nr`, `year`, `comment`) VALUES ('$otype', '$loc', '$person', '$desc', '$manu', '$model', 
            '$reg_nr', '$reg_cert_nr', '$chassis', '$year', '$comment');");
            if($sql->row_count)
                msg($Lang['success'], $Lang['success_add']);
            else
                msg($Lang['error'], $Lang['failed_add'], 'error');
        }
        else
        {
            echo '<form action="?p='.$page.'&a=add" method="post"><table id="object" border="1">';
            echo '<tr><th>'.$Lang['object_type'].'</th><td><select id="object_type" name="object_type" onchange="toogle_object();" onclick="toogle_object();" onkeyup="toogle_object();"><option value=""></option>';
            $tqry=$sql->query("SELECT id, name FROM object_type;");
            while($otype=$sql->fetch_array($tqry))
            {
                echo '<option value="'.$otype['id'].'">'.$otype['name'].'</option>';
            }
            echo '</select></td></tr>';
            echo '<tr><th>'.$Lang['loc'].'</th><td><input size="75" name="loc" value="" /></td></tr>';
            echo '<tr><th>'.$Lang['desc'].'</th><td><input size="75" name="desc" value="" /></td></tr>';
            echo '<tr><th>'.$Lang['person'].'</th><td><select name="person"><option value=""></option>';
            $pqry=$sql->query("SELECT id, name FROM clients;");
            while($person=$sql->fetch_array($pqry))
            {
                echo '<option value="'.$person['id'].'">'.$person['name'].'</option>';
            }
            echo '</select></td></tr>';
            echo '<tr style="display: none"><th>'.$Lang['manu'].'</th><td><select name="manufacturer"><option value=""></option>';
            $mqry=$sql->query("SELECT id, name FROM manufacturer;");
            while($manu=$sql->fetch_array($mqry))
            {
                echo '<option value="'.$manu['id'].'">'.$manu['name'].'</option>';
            }
            echo '</select></td></tr>';
            echo '<tr style="display: none"><th>'.$Lang['model'].'</th><td><input size="75" name="model" value="" /></td></tr>';
            echo '<tr style="display: none"><th>'.$Lang['reg_nr'].'</th><td><input size="75" name="reg_nr" value="" /></td></tr>';
            echo '<tr style="display: none"><th>'.$Lang['reg_cert_nr'].'</th><td><input size="75" name="reg_cert_nr" value="" /></td></tr>';
            echo '<tr style="display: none"><th>'.$Lang['chassis_nr'].'</th><td><input size="75" name="chassis_nr" value="" /></td></tr>';
            echo '<tr style="display: none"><th>'.$Lang['year'].'</th><td><input size="75" name="year" value="" /></td></tr>';
            echo '<tr><th>'.$Lang['comment'].'</th><td><input size="75" name="comment" value="" /></td></tr>';
            echo '</table><input type="submit" value="'.$Lang['add'].'" /></form>';
        }
    break;
    case 'view':
        if(!isset($_GET['id'])) break;
        $id=getVar('id');
        $qry=$sql->query("SELECT objects.id, objects.tId, clients.`name` as cname, object_type.name as tname, loc, cta_person, `desc`, manufacturer, model, reg_nr, reg_cert_nr, chassis_nr, year, objects.comment FROM objects INNER JOIN object_type ON(objects.tId=object_type.id) LEFT OUTER JOIN clients ON objects.cta_person=clients.id WHERE objects.id='$id'");
        $obj=$sql->fetch_array($qry);
        $show=$obj['tId']==1?'table-row':'none';
        echo '<table border="1">';
        echo '<tr><th>'.$Lang['object_type'].'</th><td>'.$obj['tname'].'</td></tr>';
        echo '<tr><th>'.$Lang['loc'].'</th><td>'.$obj['loc'].'</td></tr>';
        echo '<tr><th>'.$Lang['desc'].'</th><td>'.$obj['desc'].'</td></tr>';
        echo '<tr><th>'.$Lang['person'].'</th><td>'.$obj['cname'].'</td></tr>';
        echo '<tr style="display: '.$show.'"><th>'.$Lang['manu'].'</th><td>'.$obj['manufacturer'].'</td></tr>';
        echo '<tr style="display: '.$show.'"><th>'.$Lang['model'].'</th><td>'.$obj['model'].'</td></tr>';
        echo '<tr style="display: '.$show.'"><th>'.$Lang['reg_nr'].'</th><td>'.$obj['reg_nr'].'</td></tr>';
        echo '<tr style="display: '.$show.'"><th>'.$Lang['reg_cert_nr'].'</th><td>'.$obj['reg_cert_nr'].'</td></tr>';
        echo '<tr style="display: '.$show.'"><th>'.$Lang['chassis_nr'].'</th><td>'.$obj['chassis_nr'].'</td></tr>';
        echo '<tr style="display: '.$show.'"><th>'.$Lang['year'].'</th><td>'.$obj['year'].'</td></tr>';
        echo '<tr><th>'.$Lang['comment'].'</th><td>'.$obj['comment'].'</td></tr>';
        echo '</table>';
        echo !isset($_GET['no_head'])?'<a href="?p='.$page.'&a=edit&id='.$id.'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a>':'';
    break;
    case 'edit':
        if(!isset($_GET['id'])) break;
        $id=getVar('id');
        if($_POST)
        {
            $otype=getVar('object_type');
            $loc=getVar('loc');
            $desc=getVar('desc');
            $person=getVar('person');
            $manu=getVar('manufacturer');
            $model=getVar('model');
            $reg_nr=getVar('reg_nr');
            $reg_cert_nr=getVar('reg_cert_nr');
            $chassis=getVar('chassis_nr');
            $year=getVar('year');
            $comment=getVar('comment');
            $sql->query("UPDATE objects SET tId='$otype', loc='$loc', `desc`='$desc', cta_person='$person', manufacturer='$manu', model='$model', 
            reg_nr='$reg_nr', reg_cert_nr='$reg_cert_nr', chassis_nr='$chassis', year='$year', comment='$comment' WHERE id='$id';");
            if($sql->row_count)
                msg($Lang['success'], $Lang['success_update']);
            else
                msg($Lang['error'], $Lang['failed_update'], 'error');
        }
        else
        {
            $qry=$sql->query("SELECT objects.id, tId, object_type.name as tname, loc, cta_person, `desc`, manufacturer, model, reg_nr, reg_cert_nr, chassis_nr, year, objects.comment FROM objects INNER JOIN object_type ON(objects.tId=object_type.id) LEFT OUTER JOIN clients ON objects.cta_person=clients.id WHERE objects.id='$id'");
            $obj=$sql->fetch_array($qry);
            $show=$obj['tId']==1?'table-row':'none';
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post"><table id="object" border="1">';
            echo '<tr><th>'.$Lang['object_type'].'</th><td><select id="object_type" name="object_type" onchange="toogle_object();" onclick="toogle_object();" onkeyup="toogle_object();"><option value=""></option>';
            $tqry=$sql->query("SELECT id, name FROM object_type;");
            while($otype=$sql->fetch_array($tqry))
            {
                $sel=$otype['id']==$obj['tId']?'selected="selected"':'';
                echo '<option value="'.$otype['id'].'" '.$sel.'>'.$otype['name'].'</option>';
            }
            echo '</select></td></tr>';
            echo '<tr><th>'.$Lang['loc'].'</th><td><input size="75" name="loc" value="'.$obj['loc'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['desc'].'</th><td><input size="75" name="desc" value="'.$obj['desc'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['person'].'</th><td><select name="person"><option value=""></option>';
            $pqry=$sql->query("SELECT id, name FROM clients;");
            while($person=$sql->fetch_array($pqry))
            {
                $sel=$person['id']==$obj['cta_person']?'selected="selected"':'';
                echo '<option value="'.$person['id'].'" '.$sel.'>'.$person['name'].'</option>';
            }
            echo '</select></td></tr>';
            echo '<tr style="display: '.$show.'"><th>'.$Lang['manu'].'</th><td><select name="manufacturer"><option value=""></option>';
            $mqry=$sql->query("SELECT id, name FROM manufacturer;");
            while($manu=$sql->fetch_array($mqry))
            {
                $sel=strtolower($manu['name'])==strtolower($obj['manufacturer'])?'selected="selected"':'';
                echo '<option value="'.$manu['id'].'" '.$sel.'>'.$manu['name'].'</option>';
            }
            echo '</select></td></tr>';
            echo '<tr style="display: '.$show.'"><th>'.$Lang['model'].'</th><td><input size="75" name="model" value="'.$obj['model'].'" /></td></tr>';
            echo '<tr style="display: '.$show.'"><th>'.$Lang['reg_nr'].'</th><td><input size="75" name="reg_nr" value="'.$obj['reg_nr'].'" /></td></tr>';
            echo '<tr style="display: '.$show.'"><th>'.$Lang['reg_cert_nr'].'</th><td><input size="75" name="reg_cert_nr" value="'.$obj['reg_cert_nr'].'" /></td></tr>';
            echo '<tr style="display: '.$show.'"><th>'.$Lang['chassis_nr'].'</th><td><input size="75" name="chassis_nr" value="'.$obj['chassis_nr'].'" /></td></tr>';
            echo '<tr style="display: '.$show.'"><th>'.$Lang['year'].'</th><td><input size="75" name="year" value="'.$obj['year'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['comment'].'</th><td><input size="75" name="comment" value="'.$obj['comment'].'" /></td></tr>';
            echo '</table><input type="submit" value="'.$Lang['save'].'" /></form>';
        }
    break;
    case 'del':
        if(!isset($_GET['id'])) break;
        $id=getVar('id');
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM objects WHERE id='$id';");
            if($sql->row_count)
                msg($Lang['success'], $Lang['object_success_delete']);
            else
                msg($Lang['warning'], $Lang['object_failed_delete'], 'warning');
        }
        else
        {
            msg($Lang['warning'],$Lang['are_you_sure_delete_object'].' <br /> <a href="?p='.$page.'&a=del&id='.$id.'&confirm=1">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>"', 'warning');
            msg($Lang['warning'], $Lang['object_will_delete'], 'warning');
        }
    break;
    default:
        if(isset($_GET['pg']))
	       $start = getVar('pg');
        else 
        	$start = 1;
        if(!is_numeric($start) || $start == 0)
	       $start = 1;
        $start = abs($start) - 1;
        $startlimit = $start * getConfig('settings','items_per_page','100');
        $rows=getConfig('settings','items_per_page','100');
        $selectT=getVar('selectT','string',true);
        $selectV=getVar('selectV','string',true);
        $npage=$page;
        if($selectT !='' && $selectV!='')
        {
            $npage.='&selectT='.$selectT;
            $npage.='&selectV='.$selectV;
            $qry_add=" WHERE";
            switch($selectT)
            {
                case 'type':
                    $qry_add.=" object_type.`name` LIKE '%$selectV%' OR object_type.`abbr` LIKE '%$selectV%'";
                break;
                case 'loc':
                    $qry_add.=" `loc` LIKE '%$selectV%'";
                break;
                case 'desc':
                    $qry_add.=" `desc` LIKE '%$selectV%'";
                break;
                case 'manu':
                    $qry_add.=" `manufacturer` LIKE '%$selectV%'";
                break;
                case 'model':
                    $qry_add.=" `model` LIKE '%$selectV%'";
                break;
                case 'person':
                    $qry_add.=" `clients`.`name` LIKE '%$selectV%' OR reg_nr LIKE '%$selectV%'";
                break;
                case 'chassis':
                    $qry_add.=" `chassis_nr` LIKE '%$selectV%'";
                break;
            }
        }
        $page_foot=$sql->query("SELECT count(*) FROM objects LEFT join object_type ON(objects.tId=object_type.id) LEFT OUTER JOIN clients ON objects.cta_person=clients.id $qry_add;");
        $page_foot = $sql->result($page_foot);
        echo pagechoose($start + 1, $page_foot, $npage);
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a>';
        
        $qry=$sql->query("SELECT objects.id, clients.`name` as cname, abbr, loc, cta_person, `desc`, manufacturer, model, reg_nr, chassis_nr FROM objects INNER JOIN object_type ON(objects.tId=object_type.id) LEFT OUTER JOIN clients ON objects.cta_person=clients.id $qry_add LIMIT $startlimit, $rows");
        ?>
    <form method="get" action="index.php">
    <input type="hidden" name="p" value="<?php echo $page;?>" />
    <input type="hidden" name="pg" value="<?php echo $pg;?>" />
    <select name="selectT"><option value=""></option>
    <option value="type"<?php echo ($selectT=='type')?' selected="selected"':''; ?>><?php echo $Lang['type'];?></option>
    <option value="loc"<?php echo ($selectT=='loc')?' selected="selected"':''; ?>><?php echo $Lang['loc'];?></option>
    <option value="desc"<?php echo ($selectT=='desc')?' selected="selected"':''; ?>><?php echo $Lang['desc'];?></option>
    <option value="manu"<?php echo ($selectT=='manu')?' selected="selected"':''; ?>><?php echo $Lang['manu'];?></option>
    <option value="model"<?php echo ($selectT=='model')?' selected="selected"':''; ?>><?php echo $Lang['model'];?></option>
    <option value="person"<?php echo ($selectT=='person')?' selected="selected"':''; ?>><?php echo $Lang['person'].'/'.$Lang['reg_nr'];?></option>
    <option value="chassis"<?php echo ($selectT=='chassis')?' selected="selected"':''; ?>><?php echo $Lang['chassis_nr'];?></option>
    </select> contains <input name="selectV" type="text" value="<?php echo $selectV;?>" /><input type="submit" value="GO" /></form>
    <?php
        echo '<table border="1">';
        echo '<tr><th>'.$Lang['type'].'</th><th>'.$Lang['loc'].'</th><th>'.$Lang['desc'].'/'.$Lang['manu'].','.$Lang['model'].'</th><th>'.$Lang['person'].'/'.$Lang['reg_nr'].'</th><th>- / '.$Lang['chassis_nr'].'</th><th>'.$Lang['actions'].'</th></tr>';
        while($obj=$sql->fetch_array($qry))
        {
            echo '<tr><td>'.$obj['abbr'].'</td><td>'.$obj['loc'].'</td><td>'.$obj['desc'].' '.$obj['manufacturer'].' '.$obj['model'].'</td><td>'.$obj['cname'].' '.$obj['reg_nr'].'</td><td>'.$obj['chassis_nr'].'</td><td><a href="?p='.$page.'&a=view&id='.$obj['id'].'"><img src="img/view.png" alt="'.$Lang['view'].'" title="'.$Lang['view'].'" /></a><a href="?p='.$page.'&a=edit&id='.$obj['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=del&id='.$obj['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
        }
        echo '</table>';
        echo pagechoose($start + 1, $page_foot, $npage);
    break;
}
?>